package com.cndw.kungfu.model.activity.holiday;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.HolidayCommSerialize;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.EventNotice;
/**
 * 中秋活动数据
 * @author Everest
 *
 */
public class MidAutumnData implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1322309073198275560L;
	/**
	 * 玩家赏月指数集合
	 */
	private Map<Long, Integer> msMap = new ConcurrentHashMap<Long, Integer>();
	/**
	 * 玩家燃放烟花指数集合
	 */
	private Map<Long, Integer> fwMap = new ConcurrentHashMap<Long, Integer>();
	
	private transient List<Object[]> fwRanking = new ArrayList<Object[]>();
	
	private static int size = 10;
	
	private static int fwLastRankTime = 0;
	
	private static final int mEndTime;
	private static final int fwEndTime;
	
	static {
		int tmpMEndTime = 0;
		int tmpFwEndTime = 0;
		try {
			tmpMEndTime = TimeProcesser.getUnixTimeByString("2012-10-9 07:00:00");
			tmpFwEndTime = TimeProcesser.getUnixTimeByString("2012-10-8 23:59:59");
		} catch (Exception e) {
			e.printStackTrace();
		}
		mEndTime = tmpMEndTime;
		fwEndTime = tmpFwEndTime;
	}
	

	
	public int getMoonEndTime() {
		return mEndTime;
	}
	
	public int getFwEndTime () {
		return fwEndTime;
	}
	
	public void alterMoonScore(long playerId, int score) {
		if (msMap.containsKey(playerId)) {
			msMap.put(playerId, msMap.get(playerId) + score);
		} else {
			msMap.put(playerId, score);
		}
	}
	
	public void alterFwScore(long playerId, int score) {
		if (fwMap.containsKey(playerId)) {
			fwMap.put(playerId, fwMap.get(playerId) + score);
		} else {
			fwMap.put(playerId, score);
		}
	}
	
	public int getMoonScore(long playerId) {
		if (!msMap.containsKey(playerId)) {
			return 0;
		}
		return msMap.get(playerId);
	}
	
	public int getFwScore(long playerId) {
		if (!fwMap.containsKey(playerId)) {
			return 0;
		}
		return fwMap.get(playerId);
	}
	
	public void fwRank() {
		
		if (chkFwEnd()) {
			return;
		}
		
		if (fwRanking == null) {
			fwRanking = new ArrayList<Object[]>();
		} else {
			fwRanking.clear();
		}
		
		if (fwMap == null) {
			fwMap = new ConcurrentHashMap<Long, Integer>();
			return;
		}
		
		if (fwMap.size() <= 0) {
			return;
		}
		
		List<Entry<Long, Integer>> list = getRankList(fwMap);
		int orderId = 0;
		for (Entry<Long, Integer> e : list) {
			Player player = Context.getPlayerServer().getByPlayerId(e.getKey());
			if (player == null) {
				continue;
			}
			orderId ++;
			Object[] obj = new Object[5];
			obj[0] = orderId;
			obj[1] = e.getKey();
			obj[2] = player.getPlayerName();
			obj[3] = e.getValue();
			obj[4] = player.getVocationId();
			fwRanking.add(obj);
			if (orderId >= size) {
				break;
			}
		}
		fwLastRankTime = TimeProcesser.getUnixTime();
		if (TimeProcesser.getUnixTime() >= getFwEndTime()) {//发放称号
			int titleId = 0;
			for (int i = 0; i < 10; i++) {
				if (i >= fwRanking.size()) {//排行榜不足10人
					break;
				}
				titleId = i == 0 ? 63 : i == 1 ? 64 : i == 2 ? 65 : 66;
				Context.getGeneralServer().addTitle((Long) fwRanking.get(i)[1], titleId);
			} 
		}
	}
	
	public List<Entry<Long, Integer>> getRankList(Map<Long, Integer> data) {
		List<Entry<Long, Integer>> list = new ArrayList<Map.Entry<Long,Integer>>(data.entrySet());
		Collections.sort(list, new Comparator<Entry<Long, Integer>>() {
			@Override
			public int compare(Entry<Long, Integer> o1, Entry<Long, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		});
		return list;
	}
	
	public List<Object[]> getFwRanking() {
		if (TimeProcesser.getUnixTime() - fwLastRankTime > 300) {
			synchronized (this) {
				if (TimeProcesser.getUnixTime() - fwLastRankTime > 300) {
					fwRank();
				}
			}
		}
		return fwRanking;
	}
	
	public static void awardOthers(Collection<Long> ids, MidAutumnParams fp) {
		if (ids == null || ids.size() <= 0) {
			return;
		}
		int type = fp.getType();
		EventNotice notice = new EventNotice(ConstantsRadio.holiday, ConstantsRadio.holiday_fwEnjoy);
		notice.put("sName", fp.getSn());
		notice.put("sId", fp.getS());
		notice.put("type", type);
		notice.put("tName", fp.getTn());
		if (type == 3) {
			notice.put("ssid", fp.getSid());
		}
		Player p = null;
		int assetGet;
		int expGet;
		boolean goodsGet;
		GoodsMode goodsGetMode = Context.getGoodsServer().getModeById(ConstantsModel.fireworkAwardO[type - 1][2]);
		for (Long pId : ids) {
//			if (pId == fp.getS() || pId == fp.getT()) {
//				continue;
//			}
			p = Context.getPlayerServer().getByPlayerId(pId);
			if (p == null || !p.isOnline()) {
				continue;
			}
			assetGet = p.getLevel() * ConstantsModel.fireworkAwardO[type - 1][0];
			expGet = p.getLevel() * ConstantsModel.fireworkAwardO[type - 1][1];
			if (type == 3) {
				int coff =  p.getLevel() >= 80 ? 2 : p.getLevel() >= 40 ? 1 : 0;
				expGet = p.getLevel() * ConstantsModel.firewordExpType3[coff];
			}
			
			goodsGet = false;
			if (p.getPlayerBoxs().checkBlack(1)) {
				Context.getGoodsServer().giveGoodsBing(pId, ConstantsModel.fireworkAwardO[type - 1][2], 1, 1);
				goodsGet = true;
			}
			p.alterAsset(assetGet);
			p.alterExp(expGet);
			p.commitSync();
			
			notice.put("asset", p.getAsset());
			notice.put("assetGet", assetGet);
			notice.put("expGet", expGet);
			notice.put("goodsGet", goodsGet);
			notice.broadcast(pId);
			LogServer.recordLog(p, ConstantsLog.activity, 90443, type, assetGet, expGet, goodsGet ? goodsGetMode.getName() : null, goodsGet ? 1 : 0, fp.getS());
		}
		
	}
	
	public boolean chkMoonEnd() {
		if (TimeProcesser.getUnixTime() >= mEndTime) {
			return true;
		}
		return false;
	}
	
	public boolean chkFwEnd() {
		if (TimeProcesser.getUnixTime() >= fwEndTime) {
			return true;
		}
		return false;
	}
	
	public void commitSync() {
		HolidayCommSerialize.getInstance().commitSync();
	}

}
